﻿using MediatR;
using RBAC.ApiWrites.Applocation.Command;
using RBAC.Domain.RbacEntity;
using RBAC.Infrasturcture.Interfaces;
using RBAC.ResultCode;
using System.IO;

namespace RBAC.ApiWrites.Applocation.Commandhandler
{
    public class UpdateUserRoleCommandhandler : IRequestHandler<UpdateUserRoleCommand, ApiResult<int>>
    {
        private readonly ILogger<UpdateUserRoleCommandhandler> logger;
        private readonly IBaseRepository<User> userrepository;
        private readonly IBaseRepository<Role> rolerepository;
        private readonly IBaseRepository<UserRole> userrolerepository;

        public UpdateUserRoleCommandhandler(ILogger<UpdateUserRoleCommandhandler> logger,IBaseRepository<User> userrepository,IBaseRepository<Role> rolerepository,IBaseRepository<UserRole> userrolerepository)
        {
            this.logger = logger;
            this.userrepository = userrepository;
            this.rolerepository = rolerepository;
            this.userrolerepository = userrolerepository;
        }
        public async Task<ApiResult<int>> Handle(UpdateUserRoleCommand request, CancellationToken cancellationToken)
        {

            using (var tran = rolerepository.DB.Database.BeginTransaction()) 
            {
                try
                {
                    var userinfo = userrepository.GetAll().FirstOrDefault(x => x.UserId == request.UserId);
                    userinfo.UserName=request.UserName;
                    userinfo.UserPwd=request.UserPwd;
                    
                  var res = await userrepository.UpdateAsync(userinfo);
                    var UserRoleIdlist = userrolerepository.GetAll().Where(x => x.UserId == request.UserId).ToList();
                    await userrolerepository.DeleteRange(UserRoleIdlist);


                    List<UserRole> userrole = new List<UserRole>();
                    foreach (var item in request.RoleId)
                    {
                        userrole.Add(new UserRole()
                        {
                            UserId  = request.UserId,
                            RoleId=item,
                           

                        });
                    }
                    await userrolerepository.AddRange(userrole);

                    tran.Commit();
                    return new ApiResult<int>()
                    {
                        Code = ApiResultEnum.修改成功
                        ,
                        Message = ApiResultEnum.修改成功.ToString(),
                        Data = res
                    };
                  

                }
                catch (Exception ex)
                {
                    logger.LogError("=>" + ex.Message);

                    tran.Rollback();
                }
                return new ApiResult<int>()
                {
                    Code = ApiResultEnum.修改失败
                   ,
                    Message = ApiResultEnum.修改失败.ToString(),
                    Data = -1
                };

            }
      

        }
    }
}
